package whsw;

import java.util.Collection;
import java.util.TreeMap;

/**
 * 步骤二: 定义员工信息的存储方式
 *
 * 这个类用来存储员工信息, 并且有对员工信息增删改查功能的支持
 *
 * @author flutterdash@qq.com
 * @since 2021/6/20 13:33
 */
public class Employees {

    final TreeMap<Integer, Employee> table = new TreeMap<>();

    public Employees() {
        load();
    }

    public void load() {
        table.put(1001, new Employee(1001, "emp01", 'm', 20));
        table.put(1002, new Employee(1002, "emp01", 'm', 21));
        table.put(1003, new Employee(1003, "emp01", 'f', 20));
        table.put(1004, new Employee(1004, "emp01", 'f', 20));
    }


    /**
     * 插入一个员工信息
     * @param employee 员工对象
     * @return 是否插入成功
     */
    public boolean insert(Employee employee) {
        if (table.containsKey(employee.id)) {
            return false;
        } else {
            table.put(employee.id, employee);
            return true;
        }
    }

    /**
     * 删除
     * @param id 删除指定 id 的员工
     * @return 是否删除成功
     */
    public boolean remove(int id) {
        return table.remove(id) != null;
    }

    /**
     * @param id 员工的编号
     * @return 返回带有此 id 的员工对象
     */
    public Employee select(int id) {
        return table.get(id);
    }

    /**
     * @return 返回所有员工
     */
    public Collection<Employee> selectAll() {
        return table.values();
    }

}
